import Vue from 'vue'
import Router from 'vue-router'
import example from './example'

const layout = () => import('@/components/layout/layout'),
    exampleLayout = () => import('@/components/layout/example-layout'),
    index = () => import('@/page/index'),
    login = () => import('@/page/login'),
    // res
    registeredOrder = () => import('@/page/registered-order'),
    branchDetails = () => import('@/page/branch-details'),
    branchManage = () => import('@/page/branch-manage'),
    accountManage = () => import('@/page/account-manage'),
    memberManage = () => import('@/page/member-manage'),
    passwordEdit = () => import('@/page/password-edit'),
    appointmentSetting = () => import('@/page/appointment-setting'),

    // 初始路由集
    constantRouterMap = [
        {
            path: '/401',
            name: '401',
            component: layout
        },
        {
            path: '/',
            name: 'index',
            component: index,
            redirect: '/monitor/registered-order'
        },
        {
            path: '/login',
            name: 'login',
            component: login
        },
        {
            path: '/monitor',
            name: 'monitor',
            component: layout,
            children: [
                {
                    path: 'registered-order',
                    name: 'registeredOrder',
                    component: registeredOrder,
                    meta: {
                        title: '口罩预约情况'
                    }
                },
                {
                    path: 'branch-details',
                    name: 'branchDetails',
                    component: branchDetails,
                    meta: {
                        title: '网点详情'
                    }
                },
                {
                    path: 'branch-manage',
                    name: 'branchManage',
                    component: branchManage,
                    meta: {
                        title: '网点管理'
                    },
                    children: [
                        {
                            path: 'detail',
                            name: 'branchDetails',
                            component: branchDetails,
                            meta: {
                                title: '网点详情'
                            }
                        },
                        {
                            path: 'add',
                            name: 'branchAdd',
                            component: branchDetails,
                            meta: {
                                title: '新增网点'
                            }
                        }
                    ]
                },
                {
                    path: 'account-manage',
                    name: 'accountManage',
                    component: accountManage,
                    meta: {
                        title: '后台账户管理'
                    }
                },
                {
                    path: 'member-manage',
                    name: 'memberManage',
                    component: memberManage,
                    meta: {
                        title: '扫码人员管理'
                    }
                },
                {
                    path: 'password-edit',
                    name: 'passwordEdit',
                    component: passwordEdit,
                    meta: {
                        title: '密码管理'
                    }
                },
                {
                    path: 'appointment-setting',
                    name: 'appointmentSetting',
                    component: appointmentSetting,
                    meta: {
                        title: '预约设置'
                    }
                }
            ]
        }
    ],
    layoutRouter = []

/*
* 开发环境添加example示例路由，可跳转查看组件如何使用
* 上线时将这一段注释
*/
if (process.env.NODE_ENV !== 'production') {
    constantRouterMap.push({
        path: '/example',
        name: 'example',
        component: exampleLayout,
        meta: {
            title: '控件示例'
        },
        children: []
    })
    constantRouterMap[constantRouterMap.length - 1].children.unshift(...example)
}

let router = new Router({
    mode: 'history',
    routes: constantRouterMap
})

// 路由跳转前更改网页标题
router.beforeEach((to, from, next) => {
    // 页面跳转更改浏览器标题
    // if (to.meta && to.meta.title) {
    //     document.title = to.meta.title
    // }
    next()
})

Vue.use(Router)

export default router

export {
    layoutRouter
}
